import { isArray } from "lodash-es";

const buttonAuth:any = {
  1001: 'htscrm_activity_video_addbtn',
  1002: 'htscrm_activity_video_batch_copy_btn',
  1003: 'htscrm_activity_video_batch_delete_btn',
  1004: 'htscrm_activity_video_batch_update_btn',
  1005: 'htscrm_activity_video_edit_button',
  1006: 'htscrm_activity_video_copy_btn',
  1007: 'htscrm_activity_video_delete_btn',
  1008: 'htscrm_activity_video_problem_btn',
  1009: 'htscrm_activity_video_category_opt_btn',
  1010: 'htscrm_activity_video_category_delete_btn',
  1011: 'htscrm_activity_video_category_add_btn',
  1101: 'htscrm_activity_live_add_btn',
  1102: 'htscrm_activity_live_batch_delete_btn',
  1103: 'htscrm_activity_live_edit_btn',
  1104: 'htscrm_activity_live_delete_btn',
  1105: 'htscrm_activity_live_problem_btn',
  1106: 'htscrm_activity_chat_btn',
  1201: 'htscrm_store_add_btn',
  1202: 'htscrm_store_batch_delete_btn',
  1203: 'htscrm_store_recharge_btn',
  1204: 'htscrm_store_user_transfer_btn',
  1205: 'htscrm_store_transfer_btn',
  1206: 'htscrm_store_group_add_btn',
  1207: 'htscrm_store_group_edit_btn',
  1208: 'htscrm_store_group_delete_btn',
}

const permission: IDirective = {
  name: 'permission',
  directive: (el:any, binding:any) => {
    const { value, arg } = binding
    console.log('permission', value, arg)
    if (arg && buttonAuth[arg] && isArray(value)) {
      const hasPermission = value.includes(buttonAuth[arg])

      if(!hasPermission) {
        el.parentNode?.removeChild(el);
      }
    } else {
      throw new Error(`need roles! Like v-permission:1"['admin','editor']"`);
    }
  },
};

export default permission;
